package com.interview.net;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class MyServer2 {
	public static void main(String[] args) throws Exception {
		System.out.println("服务器启动成功.......");
		ServerSocket serverSocket = new ServerSocket(8080);
		Socket socket = serverSocket.accept();//线程阻塞:等待客户端连接
        //读取客户端发送的数据
		InputStream inputStream = socket.getInputStream();//获取输入字节流
		InputStreamReader reader = new InputStreamReader(inputStream);//字符流
		BufferedReader br = new BufferedReader(reader);//带缓冲的字符流
		String line = br.readLine();//一次读取一行
		System.out.println("服务器接到数据:"+line);
		//向客户端发送数据
		OutputStream os = socket.getOutputStream();//返回数据，发送数据给客户端
		OutputStreamWriter  osw =new OutputStreamWriter(os);//转成字符流
		osw.write("老弟辛苦:"+line);//输出字符串到客户端
		osw.close();//关闭
		serverSocket.close();//关闭服务器
	}
}